package cn.jinjimi.app.webview.utils;

import com.google.gson.reflect.TypeToken;
import cn.jinjimi.base.util.GsonUtils;
import cn.jinjimi.base.util.StringUtils;
import cn.jinjimi.base.util.URLUtil;
import cn.jinjimi.app.entity.AppConfig.AppModule;
import cn.jinjimi.app.entity.ArticleMeta;
import cn.jinjimi.app.managers.ConfigManager;
import cn.jinjimi.framework.router.RouterHelper;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author HANER
 * @date 15/4/27
 */
public class WebUtils {
    private final static String ARTICLE = "article/";
    private final static String SHARERECORDID = "shared_record_id/";
    private final static String HTML = ".html?";

    public static ArticleMeta getArticleDTOFromUrl(String url) {
        String articleId;
        ArticleMeta articleMeta = new ArticleMeta();

        if (url.contains(ARTICLE) && url.contains(HTML)) {
            if (url.contains(SHARERECORDID)) {
                articleId = url.substring(url.indexOf(ARTICLE) + ARTICLE.length(), url.indexOf(SHARERECORDID));
            } else {
                articleId = url.substring(url.indexOf(ARTICLE) + ARTICLE.length(), url.indexOf(HTML));
            }
            articleMeta.setId(articleId);
            articleMeta.setLink(url.substring(0, url.indexOf(HTML) + HTML.length() - 1));
        }

        return articleMeta;
    }

    private static long lastClickTime;

    public static boolean isDoubleClick() {
        long time = System.currentTimeMillis() - lastClickTime;
        if (0 < time && time < 1000) {
            return true;
        }
        lastClickTime = System.currentTimeMillis();
        return false;
    }

    private static List<String> mDomainList;

    public static boolean isDomain(String url) {
        if (StringUtils.isEmpty(url)) {
            return false;
        }

        String configs = ConfigManager.getInstance().getConfig(AppModule.ModuleDomains);
        if (StringUtils.isNotEmpty(configs)) {
            mDomainList = GsonUtils.fromJson(configs, new TypeToken<List<String>>() {
            }.getType());
        }

        initDomain();

        if (RouterHelper.isNativeUrl(url)) {
            return true;
        }

        URI uri = URLUtil.create(url);
        if (uri != null) {
            String host = uri.getHost();
            if (host != null) {
                for (String domain : mDomainList) {
                    String regularExpression = domain.replace("*.", "([\\w-]+[.])?");
                    if (StringUtils.isNotEmpty(regularExpression) && host.matches(regularExpression)) {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    /**
     * 只需要执行一次
     */
    static void initDomain() {
        if (mDomainList == null || mDomainList.isEmpty()) {
            mDomainList = new ArrayList<>();
            mDomainList.add("*.jinjimi.cn");
            mDomainList.add("61.177.172.35");
        }
    }
}
